<template>
  <div>
    <el-dialog :title="this.ruleForm.id ? '修改' : '新增'" :visible="showDialog" width="30%" :before-close="handleClose"
      :close-on-click-modal="false">
      <div class="content">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
          <el-form-item label="种类" prop="cropType">
            <el-select style="width: 100%;" v-model="ruleForm.cropType" filterable placeholder="请选择种类或输入关键词">
              <el-option v-for="item in cropinfoList" :key="item.value" :label="item.label" :value="item.label">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="预扣额度(%)" prop="lines">
            <el-input oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.lines" placeholder="请输入预扣额度"
              @blur="handlelines"></el-input>
          </el-form-item>
          <el-form-item label="年份" prop="year">
            <el-input oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.year" placeholder="请输入年份"
              :disabled="true"></el-input>
          </el-form-item>
        </el-form>
        <div class="footer">
          <el-button class="cancel" @click="handleClose">取 消</el-button>
          <el-button class="addButton" type="primary" @click="handleSubmit">确 定</el-button>
        </div>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { cropinfoSelectApi } from '@/api/select'
import rules from '@/utils/rules'
export default {
  props: ['showDialog'],
  data() {
    return {
      ruleForm: {
        cropType: '',
        lines: '',
        year: ''
      },
      rules: rules,
      cropinfoList: []
    };
  },
  methods: {
    handleClose() {
      this.$emit('update:showDialog', false)
      this.ruleForm = {
        cropType: '',
        lines: '',
        year: ''
      }
      this.$refs.ruleForm.resetFields();
    },
    handleSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.$emit('submit', this.ruleForm)
        } else {
          return false;
        }
      });
    },
    handlelines() {
      this.ruleForm.lines = (this.ruleForm.lines * 1).toFixed(2)
    },
    async handleGetSelect() {
      this.ruleForm.year = new Date().getFullYear()
      let res = await cropinfoSelectApi()
      this.cropinfoList = res.data
    },
  },
}
</script>

<style lang='scss' scoped>
.content {
  width: 100%;
  border: 1px solid rgb(238, 238, 238);

  .footer {
    display: flex;
    justify-content: space-between;
    box-sizing: border-box;
    padding: 0 20px;
  }

  .cancel {

    border-radius: 5px;
    position: relative;
    line-height: 20px;
    text-align: center;
    font-size: 14px;

    &:hover,
    &:focus {
      background: rgba($color: #e8f4ff, $alpha: 1) !important;
      border: 1px solid #0e7b67 !important;
      color: #0e7b67 !important;
    }

  }
}
</style>
